package com.trailwalker.inputmethod.service.data;

import com.alibaba.fastjson.JSON;
import com.trailwalker.JavaUtils.log.LogUtils;
import com.trailwalker.inputmethod.dao.DatabaseUtils;
import com.trailwalker.inputmethod.service.entity.InputMethodKeys;

import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * @author Trailwalker
 * @create 2021-09-03 13:49
 * @des
 */
public class InputMethodDataService {

    private static final String TAG = InputMethodDataService.class.getSimpleName();

    private DatabaseUtils databaseUtils;

    public InputMethodDataService() {
        databaseUtils = new DatabaseUtils("jdbc:h2:~/InputMethodDb");
    }

    public void createTable() {
        createInputMethodKeysTableSql();
    }

    public void createInputMethodKeysTableSql() {
        String createMediaInfoTableSql = "CREATE TABLE IF NOT EXISTS `InputMethodKeys`(" +
                "`id` INT UNSIGNED AUTO_INCREMENT," +
                "`name` VARCHAR(150)," +
                "`nickName` VARCHAR(150)," +
                "`desc` VARCHAR(150)," +
                "`batchGroup` VARCHAR(150)," +
                "PRIMARY KEY (`id`)" +
                ");";
        databaseUtils.createTable(createMediaInfoTableSql);
    }

    public int insertInputMethodKeys(InputMethodKeys inputMethodKeys) {
        String insertInputMethodKeysSql = "insert into InputMethodKeys values (null,'" + inputMethodKeys.getName() + "','" + inputMethodKeys.getNiceName() + "','" + inputMethodKeys.getDesc() + "','" + inputMethodKeys.getDesc() + "');";
        int count = databaseUtils.insert(insertInputMethodKeysSql);
        if (count > 0) {
            LogUtils.log(TAG, "插入 " + JSON.toJSONString(inputMethodKeys) + " 成功");
        } else {
            LogUtils.log(TAG, "插入 " + JSON.toJSONString(inputMethodKeys) + " 失败");
        }
        return count;
    }

    public int updateInputMethodKeys(InputMethodKeys inputMethodKeys) {
        String sql = "update InputMethodKeys set nickName='" + inputMethodKeys.getNiceName() + "',desc='" + inputMethodKeys.getDesc() + "',batchGroup='" + inputMethodKeys.getBatchGroup() + "' where id='" + inputMethodKeys.getId() + "';";
        int count = databaseUtils.update(sql);
        if (count > 0) {
            LogUtils.log(TAG, "更新 " + JSON.toJSONString(inputMethodKeys) + " 成功");
        } else {
            LogUtils.log(TAG, "更新 " + JSON.toJSONString(inputMethodKeys) + " 失败");
        }
        return count;
    }

    public InputMethodKeys getInputMethodKeysByName(String inputMethodKeyName) {
        String sql = "select * from InputMethodKeys where name = '" + inputMethodKeyName + "';";
        ResultSet resultSet = databaseUtils.query(sql);
        InputMethodKeys inputMethodKeys = null;
        try {
            while (resultSet.next()) {
                int id = resultSet.getInt("id");
                String name = resultSet.getString("name");
                String nickName = resultSet.getString("nickName");
                String desc = resultSet.getString("desc");
                String batchGroup = resultSet.getString("batchGroup");
                inputMethodKeys = new InputMethodKeys();
                inputMethodKeys.setId(id);
                inputMethodKeys.setName(name != null ? name : "");
                inputMethodKeys.setNiceName(nickName != null ? nickName : "");
                inputMethodKeys.setDesc(desc != null ? desc : "");
                inputMethodKeys.setBatchGroup(batchGroup != null ? batchGroup : "");
                break;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return inputMethodKeys;
    }

    public void release() {
        databaseUtils.release();
    }
}
